//
//  TopicList.m
//  TipsProject
//
//  Created by Suny on 15/5/19.
//  Copyright (c) 2015年 Suny. All rights reserved.
//

#import "TopicListViewController.h"
#import "AddTitpTittleViewController.h"
#import "TableViewCell.h"
@implementation TopicListViewController

- (TopicInfoService *)service
{
	if (!_service)
	{
		_service = [TopicInfoService serviceWithDefaultBaseUrlString];
	}
	return _service;
}


- (void)viewDidLoad
{
	[super viewDidLoad];
	self.view.backgroundColor = [UIColor colorWithHexString:@"ebebeb"];
	self.tableView.backgroundColor = [UIColor colorWithHexString:@"ebebeb"];

	self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
	[self requestDataFromNet];

}

- (void)requestDataFromNet
{
    [self.coverView startAnimating];
	
	WEAKSELF
	[self.service getTopicListWithFinishBlock:^(BaseResponse *response)
	 {

         STRONGSELF
		if(response.success)
		{
			strongSelf.modelArray = [NSMutableArray arrayWithArray:response.object];
			[strongSelf  reloadTableView];
            [strongSelf.coverView stopAnimating];
        }
		else
		{
            [strongSelf.coverView stopAnimationWithRetryAction:@selector(requestDataFromNet)
                                                 withActObject:strongSelf];

			[strongSelf showErrorMessage:response.desc];

			
		}
	}];
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath withModelInfo:(NSObject *)modelInfo
{
	TableViewCell *cell = (TableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

	TopicInfo *topicInfo = (TopicInfo *)modelInfo;
	if (topicInfo.subTopic && topicInfo.subTopic.count > 0)
	{
		NSMutableArray *topicArray = [NSMutableArray array];
		for (TopicInfo *info in topicInfo.subTopic) 
		{
			TopicTableDisplayInfo *displayInfo = [[TopicTableDisplayInfo alloc] initWithModelInfo:info];
			displayInfo.style = TopicTableViewCellContentSubStyle;
			[topicArray addObject:displayInfo];
		}
		
		[[NSNotificationCenter defaultCenter] postNotificationName:@"kShowSubTopicNotification"
															object:topicArray];
		
		[self.sideslipViewController rightItemClick];
	}
	else 
	{
		AddTitpTittleViewController *mainContentViewController = [[AddTitpTittleViewController alloc] init];
		mainContentViewController.topicInfo = topicInfo;
		mainContentViewController.hasNavigationBar = YES;

		[self.sideslipViewController.navigationController pushViewController:mainContentViewController animated:YES];
		
		[self.sideslipViewController closeSideBar];

	}
	
	
}
@end
